package com.puppet.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.puppet.RedisConst;
import com.puppet.pojo.Book;
import com.puppet.service.BookService;
import com.puppet.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
* @author Kai
* @description 针对表【t_book】的数据库操作Service实现
* @createDate 2025-07-25 10:57:18
*/
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book>
    implements BookService{
    @Autowired
    private BookMapper bookMapper;

    @Override
    @Cacheable(cacheNames = RedisConst.BOOK_LIST,keyGenerator = "keyGenerator")
    public Page<Book> findTopSales(Integer pageNum, Integer pageSize) {
        Page<Book> page = new Page<>(pageNum, pageSize);
        Wrapper<Book> wrapper = Wrappers.lambdaQuery(Book.class).orderByDesc(Book::getBuyCount);
        baseMapper.selectPage(page,wrapper);
        return page;
    }
    @Override
    @Cacheable(cacheNames = RedisConst.BOOK_LIST,keyGenerator = "keyGenerator")
    public IPage<Book> findByType(int pageNum, int pageSize, int typeId) {
        IPage<Book> page = new Page<>(pageNum,pageSize);
        Wrapper<Book> wrapper = Wrappers.lambdaQuery(Book.class)
                .eq(Book::getTypeId, typeId)
                .orderByDesc(Book::getUpdateTime);
        /*
        * select * from t_book where type_id = typeId order by update_time desc
        * */
        bookMapper.selectPage(page,wrapper);
        return page;
    }

    @Override
    @Cacheable(cacheNames = RedisConst.BOOK_DETAIL, key = "#root.methodName + ':' + #bookId")
    public Book findById(Long bookId) {
        return super.getById(bookId);
    }
}




